import Editor from "../editor";
import Operation from "./operation";
import SetSelectionOperation from "./selection_operation_set";
import RemoveTextOperation from "./text_operation_remove";

export class InsertTextOperation extends Operation {
  _spacers: string;
  _insertIndex: number;

  constructor(spacers: string, insertIndex: number) {
    super();
    this._spacers = spacers;
    this._insertIndex = insertIndex;
  }

  getSpacers () {
    return this._spacers;
  }

  setSpacers (spacers: string) {
    this._spacers = spacers;
  }

  apply(editor: Editor): void {
    editor.getTextModel()?.insert(this._insertIndex, this._spacers);
  }

  reverse(): Operation[] {
    return [
      new RemoveTextOperation(this._insertIndex, this._insertIndex + this._spacers.length),
      new SetSelectionOperation({ 
        anchor: this._insertIndex, 
        focus: this._insertIndex
      })
    ];
  }
}

export default InsertTextOperation;